//
//  ChannelDetailViewController.m
//  CapriceMusic
//
//  Created by dllo on 16/1/11.
//  Copyright © 2016年 MYX. All rights reserved.
//

#import "ChannelDetailViewController.h"
#import "SearchTableViewCell.h"
#import "ChannelModel.h"
#import "searchMVModel.h"
@interface ChannelDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) searchMVModel *channelDetailModel;

@property (nonatomic, strong) NSMutableArray *modelArray;

@end

@implementation ChannelDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.modelArray = [NSMutableArray arrayWithCapacity:0];
    self.navigationItem.title = self.channelModel.title;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [UIColor blackColor];
    [self.tableView registerClass:[SearchTableViewCell class] forCellReuseIdentifier:@"ChannelIdentifier"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self getData:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChannelIdentifier"];
    if (self.modelArray.count) {
        cell.searchMvModel = self.modelArray[indexPath.row];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.frame.size.height / 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    DetailViewController *detailVC = [[DetailViewController alloc] init];
    searchMVModel *mvpageModel = self.modelArray[indexPath.row];
    detailVC.appendingId = mvpageModel.ids;
    detailVC.type = @"VIDEO";
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)getData:(NSString *)str {
    [self.modelArray removeAllObjects];
    if ([self.channelModel.type isEqualToString:@"channel"]) {
        NSString *string = @"&detail=true&offset=0&order=VideoPubDate&size=10";
        [JEHNetWorkTool getWithUrl:[[ChannelDetailPageGETURL1  stringByAppendingString:self.channelModel.ids.description] stringByAppendingString:string]parameter:nil httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} responseType:ResponseTypeJSON success:^(id result) {
            NSArray *arr = result[@"videos"];
            for (NSDictionary *dic in arr) {
                searchMVModel *channel = [[searchMVModel alloc] initWithDictionary:dic];
                [self.modelArray addObject:channel];
            }
            [_tableView reloadData];
            [_tableView setContentOffset:CGPointMake(0, 0) animated:NO];
        } fail:^(NSError *error) {
        }];
    } else if ([self.channelModel.type isEqualToString:@"program"]) {
        NSString *string = @"&size=10";
        [JEHNetWorkTool getWithUrl:[[ChannelDetailPageGETURL2  stringByAppendingString:self.channelModel.ids.description] stringByAppendingString:string]parameter:nil httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} responseType:ResponseTypeJSON success:^(id result) {
            NSArray *arr = result[@"videos"];
            for (NSDictionary *dic in arr) {
                searchMVModel *channel = [[searchMVModel alloc] initWithDictionary:dic];
                [self.modelArray addObject:channel];
            }
            [_tableView reloadData];
            [_tableView setContentOffset:CGPointMake(0, 0) animated:NO];
        } fail:^(NSError *error) {
        }];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
